package com.kady.service.impl;

import com.kady.Dao.AdminDao;
import com.kady.Dao.impl.AdminImpl;
import com.kady.entity.ResultVO;
import com.kady.entity.User;
import com.kady.service.AdminService;
import com.kady.utils.JdbcUtil;
import com.kady.utils.MD5Util;

import java.sql.SQLException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/23
 * \* Time: 17:15
 * \* 千峰JAVA2101班
 * \* Description: 登录服务
 * \
 */

public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminImpl();

    @Override
    public Object login(String username, String password) throws SQLException {
        ResultVO resultVO = null;
        User person = adminDao.selectUser(username);
        if(person != null){
            if(!person.getPassword().equals(MD5Util.md5(password))){
                resultVO = new ResultVO(false,"密码错误");
                return resultVO;
            }
            return person;
        }else{
            resultVO = new ResultVO(false,"找不到用户信息");
            return resultVO;
        }
    }

    @Override
    public ResultVO register(User user) throws SQLException {
        ResultVO resultVO = null;
        try{
            JdbcUtil.begin();
            User finder = adminDao.selectUser(user.getUsername());

            if(finder==null){
                long res = adminDao.register(user.getUsername(),user.getPassword(),user.getNick_name(),user.getGender(),user.getPhone(),user.getEmail(),user.getUser_create_time(),user.getIs_vip(),user.getIs_admin());
                if(res>0){
                    resultVO = new ResultVO(true,"注册成功");
                }
            }else{
                resultVO = new ResultVO(false,"用户已存在");
            }
            JdbcUtil.commit();

        }catch (Exception e){
            e.printStackTrace();
            JdbcUtil.rollback();
        }
        return resultVO;
    }

}
